package com.distortiongames.level27
{
	import com.greensock.TweenLite;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	/**
	 * Game Camera.
	 * @author Ecky Putrady
	 */
	public class GCam
	{
		private var _canvas:Sprite;
		private var _bound:Rectangle;
		
		private var _lastX:Number;
		private var _lastY:Number;
		private var _lastZoomAmount:Number;
		
		/**
		 * constructor
		 * @param	canvas	the sprite that will be panned and zoomed (usually the container for your game entities)
		 * @param	bound	the boundary. GCam will pan so that no area outside the boundary are shown
		 */
		public function GCam(canvas:Sprite, bound:Rectangle)
		{
			_canvas = canvas;
			_bound = bound;
		}
		
		/**
		 * pan and zoom
		 *
		 * @param	x	the point that will be at the center of the screen
		 * @param	y	the point that will be at the center of the screen
		 * @param	zoomAmount	how much will the camera zoom? (normal zoom is 1.0)
		 * @param	duration	how long does it take to complete the pan and zoom (in secs)
		 */
		public function to(x:Number, y:Number, zoomAmount:Number, duration:Number):void
		{
			var xx:Number = x * zoomAmount;
			var yy:Number = y * zoomAmount;
			var hx:Number = _canvas.stage.stageWidth / 2;
			var hy:Number = _canvas.stage.stageHeight / 2;
			
			if (xx - _bound.left * zoomAmount < hx)
				xx = _bound.left * zoomAmount + hx;
			else if (_bound.right * zoomAmount - xx < hx)
				xx = _bound.right * zoomAmount - hx;
			
			if (yy - _bound.top * zoomAmount < hy)
				yy = _bound.top * zoomAmount + hy;
			else if (_bound.bottom * zoomAmount - yy < hy)
				yy = _bound.bottom * zoomAmount - hy;
			
			var tgtx:Number = _canvas.stage.stageWidth * 0.5 - xx;
			var tgty:Number = _canvas.stage.stageHeight * 0.5 - yy;
			
			TweenLite.to(_canvas, duration, {x: tgtx, y: tgty, scaleX: zoomAmount, scaleY: zoomAmount});
			
			_lastX = x;
			_lastY = y;
			_lastZoomAmount = zoomAmount;
		}
		
		public function get lastZoomAmount():Number
		{
			return _lastZoomAmount;
		}
		
		public function get lastY():Number
		{
			return _lastY;
		}
		
		public function get lastX():Number
		{
			return _lastX;
		}
	}

}